home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC / IMAGE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-21  |  75.6 KB  |  2,261 lines

  1. /* $Id: image.c,v 3.15 1998/08/21 02:41:39 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: image.c,v $
  26.  * Revision 3.15  1998/08/21 02:41:39  brianp
  27.  * added gl_pack/unpack_polygon_stipple()
  28.  *
  29.  * Revision 3.14  1998/08/06 01:38:47  brianp
  30.  * removed DEFARRAY/UNDEFARRAY from gl_pack_rgba_span()
  31.  *
  32.  * Revision 3.13  1998/08/01 04:53:23  brianp
  33.  * bitmap unpacking didn't distinguish GL_COLOR_INDEX from GL_STENCIL_INDEX
  34.  *
  35.  * Revision 3.12  1998/07/26 17:24:18  brianp
  36.  * replaced const with CONST because of IRIX cc warning
  37.  *
  38.  * Revision 3.11  1998/07/18 03:36:41  brianp
  39.  * removed some debugging code
  40.  *
  41.  * Revision 3.10  1998/07/18 03:33:17  brianp
  42.  * GL_INT type wasn't implemented
  43.  *
  44.  * Revision 3.9  1998/07/17 03:24:53  brianp
  45.  * added gl_pack_rgba_span()
  46.  *
  47.  * Revision 3.8  1998/06/14 15:23:08  brianp
  48.  * don't bit-flip bytes for GLubyte images
  49.  *
  50.  * Revision 3.7  1998/05/05 00:19:06  brianp
  51.  * added GL_COLOR_INDEXxx_EXT cases to gl_components_in_format()
  52.  *
  53.  * Revision 3.6  1998/03/28 03:57:58  brianp
  54.  * fixed minor IRIX cc warning
  55.  *
  56.  * Revision 3.5  1998/03/27 03:37:40  brianp
  57.  * fixed G++ warnings
  58.  *
  59.  * Revision 3.4  1998/03/15 18:50:25  brianp
  60.  * added GL_EXT_abgr extension
  61.  *
  62.  * Revision 3.3  1998/02/08 20:21:09  brianp
  63.  * fixed bitmap unpacking error
  64.  *
  65.  * Revision 3.2  1998/02/01 22:29:09  brianp
  66.  * added support for packed pixel formats
  67.  *
  68.  * Revision 3.1  1998/02/01 20:47:42  brianp
  69.  * added GL_BGR and GL_BGRA pixel formats
  70.  *
  71.  * Revision 3.0  1998/01/31 20:54:19  brianp
  72.  * initial rev
  73.  *
  74.  */
  75.  
  76.  
  77. #ifdef PC_HEADER
  78. #include "all.h"
  79. #else
  80. #include <assert.h>
  81. #include <stdlib.h>
  82. #include <string.h>
  83. #include "context.h"
  84. #include "image.h"
  85. #include "macros.h"
  86. #include "pixel.h"
  87. #include "types.h"
  88. #endif
  89.  
  90.  
  91.  
  92. /*
  93.  * Flip the 8 bits in each byte of the given array.
  94.  */
  95. void gl_flip_bytes( GLubyte *p, GLuint n )
  96. {
  97.    register GLuint i, a, b;
  98.  
  99.    for (i=0;i<n;i++) {
  100.       b = (GLuint) p[i];
  101.       a = ((b & 0x01) << 7) |
  102.       ((b & 0x02) << 5) |
  103.       ((b & 0x04) << 3) |
  104.       ((b & 0x08) << 1) |
  105.       ((b & 0x10) >> 1) |
  106.       ((b & 0x20) >> 3) |
  107.       ((b & 0x40) >> 5) |
  108.       ((b & 0x80) >> 7);
  109.       p[i] = (GLubyte) a;
  110.    }
  111. }
  112.  
  113.  
  114. /*
  115.  * Flip the order of the 2 bytes in each word in the given array.
  116.  */
  117. void gl_swap2( GLushort *p, GLuint n )
  118. {
  119.    register GLuint i;
  120.  
  121.    for (i=0;i<n;i++) {
  122.       p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
  123.    }
  124. }
  125.  
  126.  
  127.  
  128. /*
  129.  * Flip the order of the 4 bytes in each word in the given array.
  130.  */
  131. void gl_swap4( GLuint *p, GLuint n )
  132. {
  133.    register GLuint i, a, b;
  134.  
  135.    for (i=0;i<n;i++) {
  136.       b = p[i];
  137.       a =  (b >> 24)
  138.     | ((b >> 8) & 0xff00)
  139.     | ((b << 8) & 0xff0000)
  140.     | ((b << 24) & 0xff000000);
  141.       p[i] = a;
  142.    }
  143. }
  144.  
  145.  
  146.  
  147.  
  148. /*
  149.  * Return the size, in bytes, of the given GL datatype.
  150.  * Return 0 if GL_BITMAP.
  151.  * Return -1 if invalid type enum.
  152.  */
  153. GLint gl_sizeof_type( GLenum type )
  154. {
  155.    switch (type) {
  156.       case GL_BITMAP:
  157.      return 0;
  158.       case GL_UNSIGNED_BYTE:
  159.          return sizeof(GLubyte);
  160.       case GL_BYTE:
  161.      return sizeof(GLbyte);
  162.       case GL_UNSIGNED_SHORT:
  163.      return sizeof(GLushort);
  164.       case GL_SHORT:
  165.      return sizeof(GLshort);
  166.       case GL_UNSIGNED_INT:
  167.      return sizeof(GLuint);
  168.       case GL_INT:
  169.      return sizeof(GLint);
  170.       case GL_FLOAT:
  171.      return sizeof(GLfloat);
  172.       default:
  173.          return -1;
  174.    }
  175. }
  176.  
  177.  
  178. /*
  179.  * Same as gl_sizeof_packed_type() but we also accept the
  180.  * packed pixel format datatypes.
  181.  */
  182. GLint gl_sizeof_packed_type( GLenum type )
  183. {
  184.    switch (type) {
  185.       case GL_BITMAP:
  186.      return 0;
  187.       case GL_UNSIGNED_BYTE:
  188.          return sizeof(GLubyte);
  189.       case GL_BYTE:
  190.      return sizeof(GLbyte);
  191.       case GL_UNSIGNED_SHORT:
  192.      return sizeof(GLushort);
  193.       case GL_SHORT:
  194.      return sizeof(GLshort);
  195.       case GL_UNSIGNED_INT:
  196.      return sizeof(GLuint);
  197.       case GL_INT:
  198.      return sizeof(GLint);
  199.       case GL_FLOAT:
  200.      return sizeof(GLfloat);
  201.       case GL_UNSIGNED_BYTE_3_3_2:
  202.          return sizeof(GLubyte);
  203.       case GL_UNSIGNED_BYTE_2_3_3_REV:
  204.          return sizeof(GLubyte);
  205.       case GL_UNSIGNED_SHORT_5_6_5:
  206.          return sizeof(GLshort);
  207.       case GL_UNSIGNED_SHORT_5_6_5_REV:
  208.          return sizeof(GLshort);
  209.       case GL_UNSIGNED_SHORT_4_4_4_4:
  210.          return sizeof(GLshort);
  211.       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  212.          return sizeof(GLshort);
  213.       case GL_UNSIGNED_SHORT_5_5_5_1:
  214.          return sizeof(GLshort);
  215.       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  216.          return sizeof(GLshort);
  217.       case GL_UNSIGNED_INT_8_8_8_8:
  218.          return sizeof(GLuint);
  219.       case GL_UNSIGNED_INT_8_8_8_8_REV:
  220.          return sizeof(GLuint);
  221.       case GL_UNSIGNED_INT_10_10_10_2:
  222.          return sizeof(GLuint);
  223.       case GL_UNSIGNED_INT_2_10_10_10_REV:
  224.          return sizeof(GLuint);
  225.       default:
  226.          return -1;
  227.    }
  228. }
  229.  
  230.  
  231.  
  232. /*
  233.  * Return the number of components in a GL enum pixel type.
  234.  * Return -1 if bad format.
  235.  */
  236. GLint gl_components_in_format( GLenum format )
  237. {
  238.    switch (format) {
  239.       case GL_COLOR_INDEX:
  240.       case GL_COLOR_INDEX1_EXT:
  241.       case GL_COLOR_INDEX2_EXT:
  242.       case GL_COLOR_INDEX4_EXT:
  243.       case GL_COLOR_INDEX8_EXT:
  244.       case GL_COLOR_INDEX12_EXT:
  245.       case GL_COLOR_INDEX16_EXT:
  246.       case GL_STENCIL_INDEX:
  247.       case GL_DEPTH_COMPONENT:
  248.       case GL_RED:
  249.       case GL_GREEN:
  250.       case GL_BLUE:
  251.       case GL_ALPHA:
  252.       case GL_LUMINANCE:
  253.          return 1;
  254.       case GL_LUMINANCE_ALPHA:
  255.      return 2;
  256.       case GL_RGB:
  257.      return 3;
  258.       case GL_RGBA:
  259.      return 4;
  260.       case GL_BGR:
  261.      return 3;
  262.       case GL_BGRA:
  263.      return 4;
  264.       case GL_ABGR_EXT:
  265.          return 4;
  266.       /* SPECIAL CASES: */
  267.       case GL_UNSIGNED_BYTE_3_3_2:
  268.       case GL_UNSIGNED_BYTE_2_3_3_REV:
  269.       case GL_UNSIGNED_SHORT_5_6_5:
  270.       case GL_UNSIGNED_SHORT_5_6_5_REV:
  271.       case GL_UNSIGNED_SHORT_4_4_4_4:
  272.       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  273.       case GL_UNSIGNED_SHORT_5_5_5_1:
  274.       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  275.       case GL_UNSIGNED_INT_8_8_8_8:
  276.       case GL_UNSIGNED_INT_8_8_8_8_REV:
  277.       case GL_UNSIGNED_INT_10_10_10_2:
  278.       case GL_UNSIGNED_INT_2_10_10_10_REV:
  279.          return 1;
  280.       default:
  281.          return -1;
  282.    }
  283. }
  284.  
  285.  
  286. /*
  287.  * Return the address of a pixel in an image (actually a volume).
  288.  * Pixel unpacking/packing parameters are observed according to 'packing'.
  289.  * Input:  image - start of image data
  290.  *         width, height - size of image
  291.  *         format - image format
  292.  *         type - pixel component type
  293.  *         packing - GL_TRUE = use packing params
  294.  *                   GL_FALSE = use unpacking params.
  295.  *         img - which image in the volume (0 for 2-D images)
  296.  *         row, column - location of pixel in the image
  297.  * Return:  address of pixel at (image,row,column) in image or NULL if error.
  298.  */
  299. GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
  300.                                 const GLvoid *image, GLsizei width,
  301.                                 GLsizei height, GLenum format, GLenum type,
  302.                                 GLint img, GLint row, GLint column )
  303. {
  304.    GLint bytes_per_comp;   /* bytes per component */
  305.    GLint comp_per_pixel;   /* components per pixel */
  306.    GLint comps_per_row;    /* components per row */
  307.    GLint pixels_per_row;   /* pixels per row */
  308.    GLint bytes_per_image;
  309.    GLint rows_per_image;
  310.    GLint alignment;        /* 1, 2 or 4 */
  311.    GLint skiprows;
  312.    GLint skippixels;
  313.    GLint skipimages;       /* for 3-D */
  314.    GLubyte *pixel_addr;
  315.  
  316.    /* Compute bytes per component */
  317.    bytes_per_comp = gl_sizeof_packed_type( type );
  318.    if (bytes_per_comp<0) {
  319.       return NULL;
  320.    }
  321.  
  322.    /* Compute number of components per pixel */
  323.    comp_per_pixel = gl_components_in_format( format );
  324.    if (comp_per_pixel<0 && type != GL_BITMAP) {
  325.       return NULL;
  326.    }
  327.  
  328.    alignment = packing->Alignment;
  329.    if (packing->RowLength > 0) {
  330.       pixels_per_row = packing->RowLength;
  331.    }
  332.    else {
  333.       pixels_per_row = width;
  334.    }
  335.    if (packing->ImageHeight>0) {
  336.       rows_per_image = packing->ImageHeight;
  337.    }
  338.    else {
  339.       rows_per_image = height;
  340.    }
  341.    skiprows = packing->SkipRows;
  342.    skippixels = packing->SkipPixels;
  343.    skipimages = packing->SkipImages;
  344.  
  345.    if (type==GL_BITMAP) {
  346.       /* BITMAP data */
  347.       GLint bytes_per_row;
  348.  
  349.       bytes_per_row = alignment
  350.                     * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
  351.  
  352.       bytes_per_image = bytes_per_row * rows_per_image;
  353.  
  354.       pixel_addr = (GLubyte *) image
  355.                  + (skipimages + img) * bytes_per_image
  356.                  + (skiprows + row) * bytes_per_row
  357.                  + (skippixels + column) / 8;
  358.    }
  359.    else {
  360.       /* Non-BITMAP data */
  361.  
  362.       if (bytes_per_comp>=alignment) {
  363.      comps_per_row = comp_per_pixel * pixels_per_row;
  364.       }
  365.       else {
  366.          GLint bytes_per_row = bytes_per_comp * comp_per_pixel
  367.                              * pixels_per_row;
  368.  
  369.      comps_per_row = alignment / bytes_per_comp
  370.                        * CEILING( bytes_per_row, alignment );
  371.       }
  372.  
  373.       bytes_per_image = bytes_per_comp * comps_per_row * rows_per_image;
  374.  
  375.       /* Copy/unpack pixel data to buffer */
  376.       pixel_addr = (GLubyte *) image
  377.                  + (skipimages + img) * bytes_per_image
  378.                  + (skiprows + row) * bytes_per_comp * comps_per_row
  379.                  + (skippixels + column) * bytes_per_comp * comp_per_pixel;
  380.    }
  381.  
  382.    return (GLvoid *) pixel_addr;
  383. }
  384.  
  385.  
  386.  
  387. /*
  388.  * Allocate a new gl_image.  All fields are initialized to zero.
  389.  */
  390. static struct gl_image *alloc_image( void )
  391. {
  392.    return (struct gl_image *) calloc(sizeof(struct gl_image), 1);
  393. }
  394.  
  395.  
  396.  
  397. /*
  398.  * Allocate a new gl_image with the error flag set.
  399.  */
  400. static struct gl_image *alloc_error_image( GLint width, GLint height,
  401.                                            GLint depth, GLenum format,
  402.                                            GLenum type )
  403. {
  404.    struct gl_image *image = alloc_image();
  405.    if (image) {
  406.       image->Width = width;
  407.       image->Height = height;
  408.       image->Depth = depth;
  409.       image->Format = format;
  410.       image->Type = type;
  411.       image->ErrorFlag = GL_TRUE;
  412.    }
  413.    return image;
  414. }
  415.  
  416.  
  417.  
  418. /*
  419.  * Free a gl_image.
  420.  */
  421. void gl_free_image( struct gl_image *image )
  422. {
  423.    if (image->Data) {
  424.       free(image->Data);
  425.    }
  426.    free(image);
  427. }
  428.  
  429.  
  430.  
  431. /*
  432.  * Do error checking on an image.  If there's an error, register it and
  433.  * return GL_TRUE, else return GL_FALSE.
  434.  */
  435. GLboolean gl_image_error_test( GLcontext *ctx, const struct gl_image *image,
  436.                                const char *msg )
  437. {
  438.    assert(image);
  439.    if (image->Width <= 0 || image->Height <= 0 || image->Depth <= 0) {
  440.       gl_error( ctx, GL_INVALID_VALUE, msg );
  441.       return GL_TRUE;
  442.    }
  443.    else {
  444.       return GL_FALSE;
  445.    }
  446. }
  447.  
  448.  
  449.  
  450. /*
  451.  * Unpack a depth-buffer image storing values as GLshort, GLuint, or GLfloats.
  452.  * Input:  type - datatype of src depth image
  453.  * Return pointer to a new gl_image structure.
  454.  *
  455.  * Notes:  if the source image type is GLushort then the gl_image will
  456.  * also store GLushorts.  If the src image type is GLuint then the gl_image
  457.  * will also store GLuints.  For all other src image types the gl_image
  458.  * will store GLfloats.  The integer cases can later be optimized.
  459.  */
  460. static struct gl_image *unpack_depth_image( GLcontext *ctx, GLenum type,
  461.                                             GLint width, GLint height,
  462.                                             const GLvoid *pixels )
  463. {
  464.    struct gl_image *image;
  465.    GLfloat *fDst;
  466.    GLushort *sDst;
  467.    GLuint *iDst;
  468.    GLint i, j;
  469.  
  470.    image = alloc_image();
  471.    if (image) {
  472.       image->Width = width;
  473.       image->Height = height;
  474.       image->Depth = 1;
  475.       image->Components = 1;
  476.       image->Format = GL_DEPTH_COMPONENT;
  477.       if (type==GL_UNSIGNED_SHORT) {
  478.          image->Type = GL_UNSIGNED_SHORT;
  479.          image->Data = malloc( width * height * sizeof(GLushort));
  480.       }
  481.       else if (type==GL_UNSIGNED_INT) {
  482.          image->Type = GL_UNSIGNED_INT;
  483.          image->Data = malloc( width * height * sizeof(GLuint));
  484.       }
  485.       else {
  486.          image->Type = GL_FLOAT;
  487.          image->Data = malloc( width * height * sizeof(GLfloat));
  488.       }
  489.       image->RefCount = 0;
  490.       if (!image->Data)
  491.          return image;
  492.    }
  493.    else {
  494.       return NULL;
  495.    }
  496.  
  497.    fDst = (GLfloat *) image->Data;
  498.    sDst = (GLushort *) image->Data;
  499.    iDst = (GLuint *) image->Data;
  500.  
  501.    for (i=0;i<height;i++) {
  502.       GLvoid *src = gl_pixel_addr_in_image( &ctx->Unpack, pixels,
  503.                                             width, height,
  504.                                             GL_DEPTH_COMPONENT, type,
  505.                                             0, i, 0 );
  506.       if (!src) {
  507.          return image;
  508.       }
  509.  
  510.       switch (type) {
  511.          case GL_BYTE:
  512.             assert(image->Type == GL_FLOAT);
  513.             for (j=0; j<width; j++) {
  514.                *fDst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
  515.             }
  516.             break;
  517.          case GL_UNSIGNED_BYTE:
  518.             assert(image->Type == GL_FLOAT);
  519.             for (j=0; j<width; j++) {
  520.                *fDst++ = UBYTE_TO_FLOAT(((GLubyte*)src)[j]);
  521.             }
  522.             break;
  523.          case GL_UNSIGNED_SHORT:
  524.             assert(image->Type == GL_UNSIGNED_SHORT);
  525.             MEMCPY( sDst, src, width * sizeof(GLushort) );
  526.             if (ctx->Unpack.SwapBytes) {
  527.                gl_swap2( sDst, width );
  528.             }
  529.             sDst += width;
  530.             break;
  531.          case GL_SHORT:
  532.             assert(image->Type == GL_FLOAT);
  533.             if (ctx->Unpack.SwapBytes) {
  534.                for (j=0;j<width;j++) {
  535.                   GLshort value = ((GLshort*)src)[j];
  536.                   value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
  537.                   *fDst++ = SHORT_TO_FLOAT(value);
  538.                }
  539.             }
  540.             else {
  541.                for (j=0;j<width;j++) {
  542.                   *fDst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
  543.                }
  544.             }
  545.             break;
  546.          case GL_INT:
  547.             assert(image->Type == GL_FLOAT);
  548.             if (ctx->Unpack.SwapBytes) {
  549.                for (j=0;j<width;j++) {
  550.                   GLint value = ((GLint*)src)[j];
  551.                   value = ((value >> 24) & 0x000000ff) |
  552.                           ((value >> 8)  & 0x0000ff00) |
  553.                           ((value << 8)  & 0x00ff0000) |
  554.                           ((value << 24) & 0xff000000);
  555.                   *fDst++ = INT_TO_FLOAT(value);
  556.                }
  557.             }
  558.             else {
  559.                for (j=0;j<width;j++) {
  560.                   *fDst++ = INT_TO_FLOAT(((GLint*)src)[j]);
  561.                }
  562.             }
  563.             iDst += width;
  564.             break;
  565.          case GL_UNSIGNED_INT:
  566.             assert(image->Type == GL_UNSIGNED_INT);
  567.             MEMCPY( iDst, src, width * sizeof(GLuint) );
  568.             if (ctx->Unpack.SwapBytes) {
  569.                gl_swap4( iDst, width );
  570.             }
  571.             iDst += width;
  572.             break;
  573.          case GL_FLOAT:
  574.             assert(image->Type == GL_FLOAT);
  575.             MEMCPY( fDst, src, width * sizeof(GLfloat) );
  576.             if (ctx->Unpack.SwapBytes) {
  577.                gl_swap4( (GLuint*) fDst, width );
  578.             }
  579.             fDst += width;
  580.             break;
  581.          default:
  582.             gl_problem(ctx, "unpack_depth_image type" );
  583.             return image;
  584.       }
  585.    }
  586.  
  587.    return image;
  588. }
  589.  
  590.  
  591.  
  592. /*
  593.  * Unpack a stencil image.  Store as GLubytes in a gl_image structure.
  594.  * Return:  pointer to new gl_image structure.
  595.  */
  596. static struct gl_image *unpack_stencil_image( GLcontext *ctx, GLenum type,
  597.                                               GLint width, GLint height,
  598.                                               const GLvoid *pixels )
  599. {
  600.    struct gl_image *image;
  601.    GLubyte *dst;
  602.    GLint i, j;
  603.  
  604.    assert(sizeof(GLstencil) == sizeof(GLubyte));
  605.  
  606.    image = alloc_image();
  607.    if (image) {
  608.       image->Width = width;
  609.       image->Height = height;
  610.       image->Depth = 1;
  611.       image->Components = 1;
  612.       image->Format = GL_STENCIL_INDEX;
  613.       image->Type = GL_UNSIGNED_BYTE;
  614.       image->Data = malloc( width * height * sizeof(GLubyte));
  615.       image->RefCount = 0;
  616.       if (!image->Data)
  617.          return image;
  618.    }
  619.    else {
  620.       return NULL;
  621.    }
  622.  
  623.    dst = (GLubyte *) image->Data;
  624.  
  625.    for (i=0;i<height;i++) {
  626.       GLvoid *src = gl_pixel_addr_in_image( &ctx->Unpack, pixels,
  627.                                             width, height,
  628.                                             GL_STENCIL_INDEX, type,
  629.                                             0, i, 0 );
  630.       if (!src) {
  631.          return image;
  632.       }
  633.  
  634.       switch (type) {
  635.          case GL_UNSIGNED_BYTE:
  636.          case GL_BYTE:
  637.             MEMCPY( dst, src, width * sizeof(GLubyte) );
  638.             dst += width * sizeof(GLubyte);
  639.             break;
  640.          case GL_UNSIGNED_SHORT:
  641.          case GL_SHORT:
  642.             if (ctx->Unpack.SwapBytes) {
  643.                /* grab upper byte */
  644.                for (j=0; j < width; j++) {
  645.                   *dst++ = (((GLushort*)src)[j] & 0xff00) >> 8;
  646.                }
  647.             }
  648.             else {
  649.                for (j=0; j < width; j++) {
  650.                   *dst++ = (((GLushort*)src)[j]) & 0xff;
  651.                }
  652.             }
  653.             break;
  654.          case GL_INT:
  655.             if (ctx->Unpack.SwapBytes) {
  656.                /* grab upper byte */
  657.                for (j=0; j < width; j++) {
  658.                   *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
  659.                }
  660.             }
  661.             else {
  662.                for (j=0; j < width; j++) {
  663.                   *dst++ = (((GLuint*)src)[j]) & 0xff;
  664.                }
  665.             }
  666.             break;
  667.          case GL_UNSIGNED_INT:
  668.             if (ctx->Unpack.SwapBytes) {
  669.                /* grab upper byte */
  670.                for (j=0; j < width; j++) {
  671.                   *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
  672.                }
  673.             }
  674.             else {
  675.                for (j=0; j < width; j++) {
  676.                   *dst++ = (((GLuint*)src)[j]) & 0xff;
  677.                }
  678.             }
  679.             break;
  680.          case GL_FLOAT:
  681.             if (ctx->Unpack.SwapBytes) {
  682.                for (j=0; j < width; j++) {
  683.                   GLfloat fvalue;
  684.                   GLint value = ((GLuint*)src)[j];
  685.                   value = ((value & 0xff000000) >> 24)
  686.                      | ((value & 0x00ff0000) >> 8)
  687.                      | ((value & 0x0000ff00) << 8)
  688.                      | ((value & 0x000000ff) << 24);
  689.                   fvalue = *((GLfloat*) &value);
  690.                   *dst++ = ((GLint) fvalue) & 0xff;
  691.                }
  692.             }
  693.             else {
  694.                for (j=0; j < width; j++) {
  695.                   GLfloat fvalue = ((GLfloat *)src)[j];
  696.                   *dst++ = ((GLint) fvalue) & 0xff;
  697.                }
  698.             }
  699.             break;
  700.          default:
  701.             gl_problem(ctx, "unpack_stencil_image type" );
  702.             return image;
  703.       }
  704.    }
  705.  
  706.    return image;
  707. }
  708.  
  709.  
  710.  
  711. /*
  712.  * Unpack a bitmap, return a new gl_image struct.
  713.  */
  714. static struct gl_image *unpack_bitmap( GLcontext *ctx, GLenum format,
  715.                                        GLint width, GLint height,
  716.                                        const GLvoid *pixels )
  717. {
  718.    struct gl_image *image;
  719.    GLint bytes, i, width_in_bytes;
  720.    GLubyte *buffer, *dst;
  721.  
  722.    assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX);
  723.  
  724.    /* Alloc dest storage */
  725.    bytes = ((width+7)/8 * height);
  726.    if (bytes>0 && pixels!=NULL) {
  727.       buffer = (GLubyte *) malloc( bytes );
  728.       if (!buffer) {
  729.          return NULL;
  730.       }
  731.       /* Copy/unpack pixel data to buffer */
  732.       width_in_bytes = CEILING( width, 8 );
  733.       dst = buffer;
  734.       for (i=0; i<height; i++) {
  735.          GLvoid *src = gl_pixel_addr_in_image( &ctx->Unpack, pixels,
  736.                                                width, height,
  737.                                                GL_COLOR_INDEX, GL_BITMAP,
  738.                                                0, i, 0 );
  739.          if (!src) {
  740.             free(buffer);
  741.             return NULL;
  742.          }
  743.          MEMCPY( dst, src, width_in_bytes );
  744.          dst += width_in_bytes;
  745.       }
  746.       /* Bit flipping */
  747.       if (ctx->Unpack.LsbFirst) {
  748.          gl_flip_bytes( buffer, bytes );
  749.       }
  750.    }
  751.    else {
  752.       /* a 'null' bitmap */
  753.       buffer = NULL;
  754.    }
  755.    
  756.    image = alloc_image();
  757.    if (image) {
  758.       image->Width = width;
  759.       image->Height = height;
  760.       image->Depth = 1;
  761.       image->Components = 0;
  762.       image->Format = format;
  763.       image->Type = GL_BITMAP;
  764.       image->Data = buffer;
  765.       image->RefCount = 0;
  766.    }
  767.    else {
  768.       free( buffer );
  769.       return NULL;
  770.    }
  771.  
  772.    return image;
  773. }
  774.  
  775.  
  776.  
  777. /*
  778.  * Unpack a 32x32 pixel polygon stipple from user memory using the
  779.  * current pixel unpack settings.
  780.  */
  781. void gl_unpack_polygon_stipple( const GLcontext *ctx,
  782.                                 const GLubyte *pattern, GLuint dest[32] )
  783. {
  784.    GLint i;
  785.    for (i = 0; i < 32; i++) {
  786.       GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack, pattern,
  787.                                   32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
  788.       dest[i] = (src[0] << 24)
  789.               | (src[1] << 16)
  790.               | (src[2] <<  8)
  791.               | (src[3]      );
  792.    }
  793.  
  794.    /* Bit flipping within each byte */
  795.    if (ctx->Unpack.LsbFirst) {
  796.       gl_flip_bytes( (GLubyte *) dest, 32 * 4 );
  797.    }
  798. }
  799.  
  800.  
  801.  
  802. /*
  803.  * Pack polygon stipple into user memory given current pixel packing
  804.  * settings.
  805.  */
  806. void gl_pack_polygon_stipple( const GLcontext *ctx,
  807.                               const GLuint pattern[32],
  808.                               GLubyte *dest )
  809. {
  810.    GLint i;
  811.    for (i = 0; i < 32; i++) {
  812.       GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image( &ctx->Pack, dest,
  813.                                   32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
  814.       dst[0] = (pattern[i] >> 24) & 0xff;
  815.       dst[1] = (pattern[i] >> 16) & 0xff;
  816.       dst[2] = (pattern[i] >>  8) & 0xff;
  817.       dst[3] = (pattern[i]      ) & 0xff;
  818.  
  819.       /* Bit flipping within each byte */
  820.       if (ctx->Pack.LsbFirst) {
  821.          gl_flip_bytes( (GLubyte *) dst, 4 );
  822.       }
  823.    }
  824. }
  825.  
  826.  
  827.  
  828. /*
  829.  * Unpack an RGBA or CI image and store it as unsigned bytes
  830.  */
  831. static struct gl_image *unpack_ubyte_image( GLcontext *ctx,
  832.                                             GLint width, GLint height,
  833.                                             GLint depth,
  834.                                             GLenum format,
  835.                                             const GLvoid *pixels )
  836. {
  837.    struct gl_image *image;
  838.    GLint width_in_bytes;
  839.    GLint components;
  840.    GLubyte *buffer, *dst;
  841.    GLint i, d;
  842.  
  843.    components = gl_components_in_format( format );
  844.  
  845.    width_in_bytes = width * components * sizeof(GLubyte);
  846.    buffer = (GLubyte *) malloc( height * width_in_bytes * depth );
  847.    if (!buffer) {
  848.       return NULL;
  849.    }
  850.  
  851.    /* Copy/unpack pixel data to buffer */
  852.    dst = buffer;
  853.    for (d=0; d<depth; d++ ) {
  854.       for (i=0;i<height;i++) {
  855.          GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack,
  856.                        pixels, width, height, format, GL_UNSIGNED_BYTE,
  857.                        d, i, 0 );
  858.          if (!src) {
  859.             free(buffer);
  860.             return NULL;
  861.          }
  862.          MEMCPY( dst, src, width_in_bytes );
  863.          dst += width_in_bytes;
  864.       }
  865.    }
  866.    
  867.    if (format == GL_BGR) {
  868.       /* swap order of every ubyte triplet from BGR to RGB */
  869.       for (i=0; i<width*height; i++) {
  870.          GLubyte b = buffer[i*3+0];
  871.          GLubyte r = buffer[i*3+2];
  872.          buffer[i*3+0] = r;
  873.          buffer[i*3+2] = b;
  874.       }
  875.    }
  876.    else if (format == GL_BGRA) {
  877.       /* swap order of every ubyte quadruplet from BGRA to RGBA */
  878.       for (i=0; i<width*height; i++) {
  879.          GLubyte b = buffer[i*4+0];
  880.          GLubyte r = buffer[i*4+2];
  881.          buffer[i*4+0] = r;
  882.          buffer[i*4+2] = b;
  883.       }
  884.    }
  885.    else if (format == GL_ABGR_EXT) {
  886.       /* swap order of every ubyte quadruplet from ABGR to RGBA */
  887.       for (i=0; i<width*height; i++) {
  888.          GLubyte a = buffer[i*4+0];
  889.          GLubyte b = buffer[i*4+1];
  890.          GLubyte g = buffer[i*4+2];
  891.          GLubyte r = buffer[i*4+3];
  892.          buffer[i*4+0] = r;
  893.          buffer[i*4+1] = g;
  894.          buffer[i*4+2] = b;
  895.          buffer[i*4+3] = a;
  896.       }
  897.    }
  898.  
  899.  
  900.    image = alloc_image();
  901.    if (image) {
  902.       image->Width = width;
  903.       image->Height = height;
  904.       image->Depth = depth;
  905.       image->Components = components;
  906.       if (format == GL_BGR)
  907.          image->Format = GL_RGB;
  908.       else if (format == GL_BGRA)
  909.          image->Format = GL_RGBA;
  910.       else if (format == GL_ABGR_EXT)
  911.          image->Format = GL_RGBA;
  912.       else
  913.          image->Format = format;
  914.       image->Type = GL_UNSIGNED_BYTE;
  915.       image->Data = buffer;
  916.       image->RefCount = 0;
  917.    }
  918.    else {
  919.       free( buffer );
  920.    }
  921.  
  922.    return image;
  923. }
  924.  
  925.  
  926.  
  927. /*
  928.  * Unpack an RGBA image storing image as GLfloats
  929.  */
  930. static struct gl_image *unpack_float_image( GLcontext *ctx,
  931.                                             GLint width, GLint height,
  932.                                             GLint depth,
  933.                                             GLenum format, GLenum type,
  934.                                             const GLvoid *pixels )
  935. {
  936.    struct gl_image *image;
  937.    GLfloat *dst;
  938.    GLint elems_per_row;
  939.    GLint components;
  940.    GLint i, j, d;
  941.    GLboolean normalize;
  942.  
  943.    assert(type != GL_BITMAP);
  944.  
  945.    components = gl_components_in_format( format );
  946.  
  947.    elems_per_row = width * components;
  948.  
  949.    image = alloc_image();
  950.    if (image) {
  951.       image->Width = width;
  952.       image->Height = height;
  953.       image->Depth = depth;
  954.       image->Components = components;
  955.       if (format == GL_BGR)
  956.          image->Format = GL_RGB;
  957.       else if (format == GL_BGRA)
  958.          image->Format = GL_RGBA;
  959.       else if (format == GL_ABGR_EXT)
  960.          image->Format = GL_RGBA;
  961.       else
  962.          image->Format = format;
  963.       image->Type = GL_FLOAT;
  964.       image->Data = malloc( elems_per_row * height * depth * sizeof(GLfloat));
  965.       image->RefCount = 0;
  966.       if (!image->Data)
  967.          return image;
  968.    }
  969.    else {
  970.       return NULL;
  971.    }
  972.  
  973.    normalize = (format != GL_COLOR_INDEX) && (format != GL_STENCIL_INDEX);
  974.  
  975.    dst = (GLfloat *) image->Data;
  976.  
  977.    for (d=0; d<depth; d++) {
  978.       for (i=0;i<height;i++) {
  979.          GLvoid *src = gl_pixel_addr_in_image( &ctx->Unpack, pixels,
  980.                                                width, height,
  981.                                                format, type,
  982.                                                d, i, 0 );
  983.          if (!src) {
  984.             return image;
  985.          }
  986.  
  987.          switch (type) {
  988.             case GL_UNSIGNED_BYTE:
  989.                {
  990.                   GLubyte *ubsrc = (GLubyte *) src;
  991.                   if (normalize) {
  992.                      for (j=0;j<elems_per_row;j++) {
  993.                         *dst++ = UBYTE_TO_FLOAT(ubsrc[j]);
  994.                      }
  995.                   }
  996.                   else {
  997.                      for (j=0;j<elems_per_row;j++) {
  998.                         *dst++ = (GLfloat) ubsrc[j];
  999.                      }
  1000.                   }
  1001.                }
  1002.                break;
  1003.             case GL_BYTE:
  1004.                if (normalize) {
  1005.                   for (j=0;j<elems_per_row;j++) {
  1006.                      *dst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
  1007.                   }
  1008.                }
  1009.                else {
  1010.                   for (j=0;j<elems_per_row;j++) {
  1011.                      *dst++ = (GLfloat) ((GLbyte*)src)[j];
  1012.                   }
  1013.                }
  1014.                break;
  1015.             case GL_UNSIGNED_SHORT:
  1016.                if (ctx->Unpack.SwapBytes) {
  1017.                   for (j=0;j<elems_per_row;j++) {
  1018.                      GLushort value = ((GLushort*)src)[j];
  1019.                      value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
  1020.                      if (normalize) {
  1021.                         *dst++ = USHORT_TO_FLOAT(value);
  1022.                      }
  1023.                      else {
  1024.                         *dst++ = (GLfloat) value;
  1025.                      }
  1026.                   }
  1027.                }
  1028.                else {
  1029.                   if (normalize) {
  1030.                      for (j=0;j<elems_per_row;j++) {
  1031.                         *dst++ = USHORT_TO_FLOAT(((GLushort*)src)[j]);
  1032.                      }
  1033.                   }
  1034.                   else {
  1035.                      for (j=0;j<elems_per_row;j++) {
  1036.                         *dst++ = (GLfloat) ((GLushort*)src)[j];
  1037.                      }
  1038.                   }
  1039.                }
  1040.                break;
  1041.             case GL_SHORT:
  1042.                if (ctx->Unpack.SwapBytes) {
  1043.                   for (j=0;j<elems_per_row;j++) {
  1044.                      GLshort value = ((GLshort*)src)[j];
  1045.                      value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
  1046.                      if (normalize) {
  1047.                         *dst++ = SHORT_TO_FLOAT(value);
  1048.                      }
  1049.                      else {
  1050.                         *dst++ = (GLfloat) value;
  1051.                      }
  1052.                   }
  1053.                }
  1054.                else {
  1055.                   if (normalize) {
  1056.                      for (j=0;j<elems_per_row;j++) {
  1057.                         *dst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
  1058.                      }
  1059.                   }
  1060.                   else {
  1061.                      for (j=0;j<elems_per_row;j++) {
  1062.                         *dst++ = (GLfloat) ((GLshort*)src)[j];
  1063.                      }
  1064.                   }
  1065.                }
  1066.                break;
  1067.             case GL_UNSIGNED_INT:
  1068.                if (ctx->Unpack.SwapBytes) {
  1069.                   GLuint value;
  1070.                   for (j=0;j<elems_per_row;j++) {
  1071.                      value = ((GLuint*)src)[j];
  1072.                      value = ((value & 0xff000000) >> 24)
  1073.                            | ((value & 0x00ff0000) >> 8)
  1074.                            | ((value & 0x0000ff00) << 8)
  1075.                            | ((value & 0x000000ff) << 24);
  1076.                      if (normalize) {
  1077.                         *dst++ = UINT_TO_FLOAT(value);
  1078.                      }
  1079.                      else {
  1080.                         *dst++ = (GLfloat) value;
  1081.                      }
  1082.                   }
  1083.                }
  1084.                else {
  1085.                   if (normalize) {
  1086.                      for (j=0;j<elems_per_row;j++) {
  1087.                         *dst++ = UINT_TO_FLOAT(((GLuint*)src)[j]);
  1088.                      }
  1089.                   }
  1090.                   else {
  1091.                      for (j=0;j<elems_per_row;j++) {
  1092.                         *dst++ = (GLfloat) ((GLuint*)src)[j];
  1093.                      }
  1094.                   }
  1095.                }
  1096.                break;
  1097.             case GL_INT:
  1098.                if (ctx->Unpack.SwapBytes) {
  1099.                   GLint value;
  1100.                   for (j=0;j<elems_per_row;j++) {
  1101.                      value = ((GLint*)src)[j];
  1102.                      value = ((value & 0xff000000) >> 24)
  1103.                            | ((value & 0x00ff0000) >> 8)
  1104.                            | ((value & 0x0000ff00) << 8)
  1105.                            | ((value & 0x000000ff) << 24);
  1106.                      if (normalize) {
  1107.                         *dst++ = INT_TO_FLOAT(value);
  1108.                      }
  1109.                      else {
  1110.                         *dst++ = (GLfloat) value;
  1111.                      }
  1112.                   }
  1113.                }
  1114.                else {
  1115.                   if (normalize) {
  1116.                      for (j=0;j<elems_per_row;j++) {
  1117.                         *dst++ = INT_TO_FLOAT(((GLint*)src)[j]);
  1118.                      }
  1119.                   }
  1120.                   else {
  1121.                      for (j=0;j<elems_per_row;j++) {
  1122.                         *dst++ = (GLfloat) ((GLint*)src)[j];
  1123.                      }
  1124.                   }
  1125.                }
  1126.                break;
  1127.             case GL_FLOAT:
  1128.                if (ctx->Unpack.SwapBytes) {
  1129.                   GLint value;
  1130.                   for (j=0;j<elems_per_row;j++) {
  1131.                      value = ((GLuint*)src)[j];
  1132.                      value = ((value & 0xff000000) >> 24)
  1133.                            | ((value & 0x00ff0000) >> 8)
  1134.                            | ((value & 0x0000ff00) << 8)
  1135.                            | ((value & 0x000000ff) << 24);
  1136.                      *dst++ = *((GLfloat*) &value);
  1137.                   }
  1138.                }
  1139.                else {
  1140.                   MEMCPY( dst, src, elems_per_row*sizeof(GLfloat) );
  1141.                   dst += elems_per_row;
  1142.                }
  1143.                break;
  1144.             case GL_UNSIGNED_BYTE_3_3_2:
  1145.                {
  1146.                   GLubyte *ubsrc = (GLubyte *) src;
  1147.                   for (j=0;j<elems_per_row;j++) {
  1148.                      GLubyte p = ubsrc[j];
  1149.                      *dst++ = ((p >> 5)      ) * (1.0F / 7.0F); /* red */
  1150.                      *dst++ = ((p >> 2) & 0x7) * (1.0F / 7.0F); /* green */
  1151.                      *dst++ = ((p     ) & 0x3) * (1.0F / 3.0F); /* blue */
  1152.                   }
  1153.                }
  1154.                break;
  1155.             case GL_UNSIGNED_BYTE_2_3_3_REV:
  1156.                {
  1157.                   GLubyte *ubsrc = (GLubyte *) src;
  1158.                   for (j=0;j<elems_per_row;j++) {
  1159.                      GLubyte p = ubsrc[j];
  1160.                      *dst++ = ((p     ) & 0x7) * (1.0F / 7.0F); /* red */
  1161.                      *dst++ = ((p >> 3) & 0x7) * (1.0F / 7.0F); /* green */
  1162.                      *dst++ = ((p >> 6)      ) * (1.0F / 3.0F); /* blue */
  1163.                   }
  1164.                }
  1165.                break;
  1166.             case GL_UNSIGNED_SHORT_5_6_5:
  1167.                {
  1168.                   GLushort *ussrc = (GLushort *) src;
  1169.                   for (j=0;j<elems_per_row;j++) {
  1170.                      GLushort p = ussrc[j];
  1171.                      *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* red */
  1172.                      *dst++ = ((p >>  5) & 0x3f) * (1.0F / 63.0F); /* green */
  1173.                      *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* blue */
  1174.                   }
  1175.                }
  1176.                break;
  1177.             case GL_UNSIGNED_SHORT_5_6_5_REV:
  1178.                {
  1179.                   GLushort *ussrc = (GLushort *) src;
  1180.                   for (j=0;j<elems_per_row;j++) {
  1181.                      GLushort p = ussrc[j];
  1182.                      *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* red */
  1183.                      *dst++ = ((p >>  5) & 0x3f) * (1.0F / 63.0F); /* green */
  1184.                      *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* blue */
  1185.                   }
  1186.                }
  1187.                break;
  1188.         case GL_UNSIGNED_SHORT_4_4_4_4:
  1189.                {
  1190.                   GLushort *ussrc = (GLushort *) src;
  1191.                   for (j=0;j<elems_per_row;j++) {
  1192.                      GLushort p = ussrc[j];
  1193.                      *dst++ = ((p >> 12)      ) * (1.0F / 15.0F); /* red */
  1194.                      *dst++ = ((p >>  8) & 0xf) * (1.0F / 15.0F); /* green */
  1195.                      *dst++ = ((p >>  4) & 0xf) * (1.0F / 15.0F); /* blue */
  1196.                      *dst++ = ((p      ) & 0xf) * (1.0F / 15.0F); /* alpha */
  1197.                   }
  1198.                }
  1199.                break;
  1200.         case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  1201.                {
  1202.                   GLushort *ussrc = (GLushort *) src;
  1203.                   for (j=0;j<elems_per_row;j++) {
  1204.                      GLushort p = ussrc[j];
  1205.                      *dst++ = ((p      ) & 0xf) * (1.0F / 15.0F); /* red */
  1206.                      *dst++ = ((p >>  4) & 0xf) * (1.0F / 15.0F); /* green */
  1207.                      *dst++ = ((p >>  8) & 0xf) * (1.0F / 15.0F); /* blue */
  1208.                      *dst++ = ((p >> 12)      ) * (1.0F / 15.0F); /* alpha */
  1209.                   }
  1210.                }
  1211.                break;
  1212.         case GL_UNSIGNED_SHORT_5_5_5_1:
  1213.                {
  1214.                   GLushort *ussrc = (GLushort *) src;
  1215.                   for (j=0;j<elems_per_row;j++) {
  1216.                      GLushort p = ussrc[j];
  1217.                      *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* red */
  1218.                      *dst++ = ((p >>  6) & 0x1f) * (1.0F / 31.0F); /* green */
  1219.                      *dst++ = ((p >>  1) & 0x1f) * (1.0F / 31.0F); /* blue */
  1220.                      *dst++ = ((p      ) & 0x1)  * (1.0F /  1.0F); /* alpha */
  1221.                   }
  1222.                }
  1223.                break;
  1224.         case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  1225.                {
  1226.                   GLushort *ussrc = (GLushort *) src;
  1227.                   for (j=0;j<elems_per_row;j++) {
  1228.                      GLushort p = ussrc[j];
  1229.                      *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* red */
  1230.                      *dst++ = ((p >>  5) & 0x1f) * (1.0F / 31.0F); /* green */
  1231.                      *dst++ = ((p >> 10) & 0x1f) * (1.0F / 31.0F); /* blue */
  1232.                      *dst++ = ((p >> 15)       ) * (1.0F /  1.0F); /* alpha */
  1233.                   }
  1234.                }
  1235.                break;
  1236.         case GL_UNSIGNED_INT_8_8_8_8:
  1237.                {
  1238.                   GLuint *uisrc = (GLuint *) src;
  1239.                   for (j=0;j<elems_per_row;j++) {
  1240.                      GLuint p = uisrc[j];
  1241.                      *dst++ = ((p >> 24)       ) * (1.0F / 255.0F); /* red */
  1242.                      *dst++ = ((p >> 16) & 0xff) * (1.0F / 255.0F); /* green */
  1243.                      *dst++ = ((p >>  8) & 0xff) * (1.0F / 255.0F); /* blue */
  1244.                      *dst++ = ((p      ) & 0xff) * (1.0F /   1.0F); /* alpha */
  1245.                   }
  1246.                }
  1247.                break;
  1248.         case GL_UNSIGNED_INT_8_8_8_8_REV:
  1249.                {
  1250.                   GLuint *uisrc = (GLuint *) src;
  1251.                   for (j=0;j<elems_per_row;j++) {
  1252.                      GLuint p = uisrc[j];
  1253.                      *dst++ = ((p      ) & 0xff) * (1.0F /   1.0F); /* red */
  1254.                      *dst++ = ((p >>  8) & 0xff) * (1.0F / 255.0F); /* green */
  1255.                      *dst++ = ((p >> 16) & 0xff) * (1.0F / 255.0F); /* blue */
  1256.                      *dst++ = ((p >> 24)       ) * (1.0F / 255.0F); /* alpha */
  1257.                   }
  1258.                }
  1259.                break;
  1260.         case GL_UNSIGNED_INT_10_10_10_2:
  1261.                {
  1262.                   GLuint *uisrc = (GLuint *) src;
  1263.                   for (j=0;j<elems_per_row;j++) {
  1264.                      GLuint p = uisrc[j];
  1265.                      *dst++ = ((p >> 22)        ) * (1.0F / 1023.0F); /* red */
  1266.                      *dst++ = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F); /* green */
  1267.                      *dst++ = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F); /* blue */
  1268.                      *dst++ = ((p      ) & 0x3  ) * (1.0F /    3.0F); /* alpha */
  1269.                   }
  1270.                }
  1271.                break;
  1272.         case GL_UNSIGNED_INT_2_10_10_10_REV:
  1273.                {
  1274.                   GLuint *uisrc = (GLuint *) src;
  1275.                   for (j=0;j<elems_per_row;j++) {
  1276.                      GLuint p = uisrc[j];
  1277.                      *dst++ = ((p      ) & 0x3ff) * (1.0F / 1023.0F); /* red */
  1278.                      *dst++ = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F); /* green */
  1279.                      *dst++ = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F); /* blue */
  1280.                      *dst++ = ((p >> 30)        ) * (1.0F /    3.0F); /* alpha */
  1281.                   }
  1282.                }
  1283.                break;
  1284.             default:
  1285.                gl_problem(ctx, "unpack_float_image type" );
  1286.                return image;
  1287.          }
  1288.       }
  1289.    }
  1290.  
  1291.    if (format == GL_BGR) {
  1292.       /* swap order of every float triplet from BGR to RGBA */
  1293.       GLfloat *buffer = (GLfloat *) image->Data;
  1294.       for (i=0; i<width*height*depth; i++) {
  1295.          GLfloat b = buffer[i*3+0];
  1296.          GLfloat r = buffer[i*3+2];
  1297.          buffer[i*3+0] = r;
  1298.          buffer[i*3+2] = b;
  1299.       }
  1300.    }
  1301.    else if (format == GL_BGRA) {
  1302.       /* swap order of every float quadruplet from BGRA to RGBA */
  1303.       GLfloat *buffer = (GLfloat *) image->Data;
  1304.       for (i=0; i<width*height*depth; i++) {
  1305.          GLfloat b = buffer[i*4+0];
  1306.          GLfloat r = buffer[i*4+2];
  1307.          buffer[i*4+0] = r;
  1308.          buffer[i*4+2] = b;
  1309.       }
  1310.    }
  1311.    else if (format == GL_ABGR_EXT) {
  1312.       /* swap order of every float quadruplet from ABGR to RGBA */
  1313.       GLfloat *buffer = (GLfloat *) image->Data;
  1314.       for (i=0; i<width*height*depth; i++) {
  1315.          GLfloat a = buffer[i*4+0];
  1316.          GLfloat b = buffer[i*4+1];
  1317.          GLfloat g = buffer[i*4+2];
  1318.          GLfloat r = buffer[i*4+3];
  1319.          buffer[i*4+0] = r;
  1320.          buffer[i*4+1] = g;
  1321.          buffer[i*4+2] = b;
  1322.          buffer[i*4+3] = a;
  1323.       }
  1324.    }
  1325.  
  1326.    return image;
  1327. }
  1328.  
  1329.  
  1330.  
  1331. /*
  1332.  * Unpack a bitmap image, making a new gl_image.
  1333.  */
  1334. struct gl_image *gl_unpack_bitmap( GLcontext *ctx,
  1335.                                    GLsizei width, GLsizei height,
  1336.                                    const GLubyte *bitmap )
  1337. {
  1338.    return gl_unpack_image( ctx, width, height,
  1339.                            GL_COLOR_INDEX, GL_BITMAP, bitmap );
  1340. }
  1341.  
  1342.  
  1343.  
  1344. /*
  1345.  * Unpack a 2-D image from user-supplied address, returning a pointer to
  1346.  * a new gl_image struct.
  1347.  *
  1348.  * Input:  width, height - size in pixels
  1349.  *         format - format of incoming pixel data, ignored if
  1350.  *                     srcType BITMAP.
  1351.  *         type - GL_UNSIGNED_BYTE .. GL_FLOAT
  1352.  *         pixels - pointer to unpacked image in client memory space.
  1353.  */
  1354. struct gl_image *gl_unpack_image( GLcontext *ctx,
  1355.                                   GLint width, GLint height,
  1356.                                   GLenum format, GLenum type,
  1357.                                   const GLvoid *pixels )
  1358.    return gl_unpack_image3D( ctx, width, height, 1,
  1359.                              format, type, pixels );
  1360. }
  1361.  
  1362.  
  1363.  
  1364. /* 
  1365.  * Unpack a 1, 2 or 3-D image from user-supplied address, returning a
  1366.  * pointer to a new gl_image struct.
  1367.  * This function is always called by a higher-level unpack function such
  1368.  * as gl_unpack_texsubimage() or gl_unpack_bitmap().
  1369.  *
  1370.  * Input:  width, height, depth - size in pixels
  1371.  *         format - format of incoming pixel data
  1372.  *         type - GL_UNSIGNED_BYTE .. GL_FLOAT
  1373.  *         pixels - pointer to unpacked image.
  1374.  */
  1375. struct gl_image *gl_unpack_image3D( GLcontext *ctx,
  1376.                                     GLint width, GLint height, GLint depth,
  1377.                                     GLenum format, GLenum type,
  1378.                                     const GLvoid *pixels )
  1379. {
  1380.    if (width <= 0 || height <= 0 || depth <= 0) {
  1381.       return alloc_error_image(width, height, depth, format, type);
  1382.    }
  1383.  
  1384.    if (type==GL_BITMAP) {
  1385.       if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) {
  1386.          return alloc_error_image(width, height, depth, format, type);
  1387.       }
  1388.       else {
  1389.          return unpack_bitmap( ctx, format, width, height, pixels );
  1390.       }
  1391.    }
  1392.    else if (format==GL_DEPTH_COMPONENT) {
  1393.       /* TODO: pack as GLdepth values (GLushort or GLuint) */
  1394.       return unpack_depth_image( ctx, type, width, height, pixels);
  1395.    }
  1396.    else if (format==GL_STENCIL_INDEX) {
  1397.       /* TODO: pack as GLstencil (GLubyte or GLushort) */
  1398.       return unpack_stencil_image( ctx, type, width, height, pixels);
  1399.    }
  1400.    else if (type==GL_UNSIGNED_BYTE) {
  1401.       /* upack, convert to GLubytes */
  1402.       return unpack_ubyte_image( ctx, width, height, depth, format, pixels );
  1403.    }
  1404.    else {
  1405.       /* upack, convert to floats */
  1406.       return unpack_float_image( ctx, width, height, depth,
  1407.                                  format, type, pixels );
  1408.    }
  1409.  
  1410.    /* never get here */
  1411.    /*return NULL;*/
  1412. }
  1413.  
  1414.  
  1415. /*
  1416.  * Apply pixel-transfer operations (scale, bias, mapping) to a single row
  1417.  * of a gl_image.  Put resulting color components into result array.
  1418.  */
  1419. void gl_scale_bias_map_image_data( const GLcontext *ctx,
  1420.                                    const struct gl_image *image,
  1421.                                    GLint row, GLubyte result[] )
  1422. {
  1423.    GLint start, i;
  1424.  
  1425.    assert(ctx);
  1426.    assert(image);
  1427.    assert(result);
  1428.    assert(row >= 0);
  1429.  
  1430.    start = row * image->Width * image->Components;
  1431.  
  1432.    for (i=0; i < image->Width; i++) {
  1433.       GLint pos = start+i;
  1434.       GLfloat red, green, blue, alpha;
  1435.       if (image->Type == GL_UNSIGNED_BYTE) {
  1436.          const GLubyte *data = (GLubyte *) image->Data;
  1437.          switch (image->Format) {
  1438.             case GL_RED:
  1439.                red   = data[pos] * (1.0F/255.0F);
  1440.                green = 0;
  1441.                blue  = 0;
  1442.                alpha = 0;
  1443.                break;
  1444.             case GL_RGB:
  1445.                red   = data[pos*3+0] * (1.0F/255.0F);
  1446.                green = data[pos*3+1] * (1.0F/255.0F);
  1447.                blue  = data[pos*3+2] * (1.0F/255.0F);
  1448.                alpha = 0;
  1449.                break;
  1450.             default:
  1451.                gl_problem(ctx, "bad image format in gl_scale...image_data");
  1452.                return;
  1453.          }
  1454.       }
  1455.       else if (image->Type == GL_FLOAT) {
  1456.          const GLubyte *data = (GLubyte *) image->Data;
  1457.          switch (image->Format) {
  1458.             case GL_RED:
  1459.                red   = data[pos];
  1460.                green = 0;
  1461.                blue  = 0;
  1462.                alpha = 0;
  1463.                break;
  1464.             case GL_RGB:
  1465.                red   = data[pos*3+0];
  1466.                green = data[pos*3+1];
  1467.                blue  = data[pos*3+2];
  1468.                alpha = 0;
  1469.                break;
  1470.             default:
  1471.                gl_problem(ctx, "bad image format in gl_scale...image_data");
  1472.                return;
  1473.          }
  1474.       }
  1475.       else {
  1476.          gl_problem(ctx, "Bad image type in gl_scale_...image_data");
  1477.          return;
  1478.       }
  1479.  
  1480.       assert(red   >= 0.0 && red   <= 1.0);
  1481.       assert(green >= 0.0 && green <= 1.0);
  1482.       assert(blue  >= 0.0 && blue  <= 1.0);
  1483.       assert(alpha >= 0.0 && alpha <= 1.0);
  1484.  
  1485.       /*
  1486.       if (scale or bias) {
  1487.  
  1488.  
  1489.       }
  1490.       if (mapping) {
  1491.  
  1492.       }
  1493.       */
  1494.  
  1495.       result[i*4+0] = (GLubyte) (red   * 255.0);
  1496.       result[i*4+1] = (GLubyte) (green * 255.0);
  1497.       result[i*4+2] = (GLubyte) (blue  * 255.0);
  1498.       result[i*4+3] = (GLubyte) (alpha * 255.0);
  1499.    }
  1500. }
  1501.  
  1502.  
  1503.  
  1504. /*
  1505.  * Pack the given RGBA span into client memory at 'dest' address
  1506.  * in the given pixel format and type.  Apply all enabled pixel
  1507.  * transfer and packing parameters.  This is used by glReadPixels (NOT YET)
  1508.  * and glGetTexImage?D()
  1509.  */
  1510. void gl_pack_rgba_span( const GLcontext *ctx,
  1511.                         GLuint n, CONST GLubyte rgba[][4],
  1512.                         GLenum format, GLenum type, GLvoid *destination)
  1513. {
  1514.    /* Test for optimized case first */
  1515.    if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
  1516.        format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
  1517.       /* simple case */
  1518.       MEMCPY( destination, rgba, n * 4 * sizeof(GLubyte) );
  1519.    }
  1520.    else {
  1521.       GLfloat red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH];
  1522.       GLfloat alpha[MAX_WIDTH], luminance[MAX_WIDTH];
  1523.       GLfloat rscale = 1.0F / 255.0F;
  1524.       GLfloat gscale = 1.0F / 255.0F;
  1525.       GLfloat bscale = 1.0F / 255.0F;
  1526.       GLfloat ascale = 1.0F / 255.0F;
  1527.       GLuint i;
  1528.  
  1529.       assert( n < MAX_WIDTH );
  1530.  
  1531.       /* convert color components to floating point */
  1532.       for (i=0;i<n;i++) {
  1533.          red[i]   = rgba[i][RCOMP] * rscale;
  1534.          green[i] = rgba[i][GCOMP] * gscale;
  1535.          blue[i]  = rgba[i][BCOMP] * bscale;
  1536.          alpha[i] = rgba[i][ACOMP] * ascale;
  1537.       }
  1538.  
  1539.       /*
  1540.        * Apply scale, bias and mappings if enabled.
  1541.        */
  1542.       if (ctx->Pixel.ScaleOrBiasRGBA) {
  1543.          gl_scale_and_bias_color( ctx, n, red, green, blue, alpha );
  1544.       }
  1545.       if (ctx->Pixel.MapColorFlag) {
  1546.          gl_map_color( ctx, n, red, green, blue, alpha );
  1547.       }
  1548.       if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
  1549.          for (i=0;i<n;i++) {
  1550.             GLfloat sum = red[i] + green[i] + blue[i];
  1551.             luminance[i] = CLAMP( sum, 0.0F, 1.0F );
  1552.          }
  1553.       }
  1554.  
  1555.       /*
  1556.        * Pack/store the pixels.  Ugh!  Lots of cases!!!
  1557.        */
  1558.       switch (type) {
  1559.          case GL_UNSIGNED_BYTE:
  1560.             {
  1561.                GLubyte *dst = (GLubyte *) destination;
  1562.                switch (format) {
  1563.                   case GL_RED:
  1564.                      for (i=0;i<n;i++)
  1565.                         dst[i] = FLOAT_TO_UBYTE(red[i]);
  1566.                      break;
  1567.                   case GL_GREEN:
  1568.                      for (i=0;i<n;i++)
  1569.                         dst[i] = FLOAT_TO_UBYTE(green[i]);
  1570.                      break;
  1571.                   case GL_BLUE:
  1572.                      for (i=0;i<n;i++)
  1573.                         dst[i] = FLOAT_TO_UBYTE(blue[i]);
  1574.                      break;
  1575.                   case GL_LUMINANCE:
  1576.                      for (i=0;i<n;i++)
  1577.                         dst[i] = FLOAT_TO_UBYTE(luminance[i]);
  1578.                      break;
  1579.                   case GL_LUMINANCE_ALPHA:
  1580.                      for (i=0;i<n;i++) {
  1581.                         dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
  1582.                         dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]);
  1583.                      }
  1584.                      break;
  1585.                   case GL_RGB:
  1586.                      for (i=0;i<n;i++) {
  1587.                         dst[i*3+0] = FLOAT_TO_UBYTE(red[i]);
  1588.                         dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
  1589.                         dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]);
  1590.                      }
  1591.                      break;
  1592.                   case GL_RGBA:
  1593.                      for (i=0;i<n;i++) {
  1594.                         dst[i*4+0] = FLOAT_TO_UBYTE(red[i]);
  1595.                         dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
  1596.                         dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]);
  1597.                         dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
  1598.                      }
  1599.                      break;
  1600.                   case GL_BGR:
  1601.                      for (i=0;i<n;i++) {
  1602.                         dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]);
  1603.                         dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
  1604.                         dst[i*3+2] = FLOAT_TO_UBYTE(red[i]);
  1605.                      }
  1606.                      break;
  1607.                   case GL_BGRA:
  1608.                      for (i=0;i<n;i++) {
  1609.                         dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]);
  1610.                         dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
  1611.                         dst[i*4+2] = FLOAT_TO_UBYTE(red[i]);
  1612.                         dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
  1613.                      }
  1614.                      break;
  1615.                   case GL_ABGR_EXT:
  1616.                      for (i=0;i<n;i++) {
  1617.                         dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]);
  1618.                         dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]);
  1619.                         dst[i*4+2] = FLOAT_TO_UBYTE(green[i]);
  1620.                         dst[i*4+3] = FLOAT_TO_UBYTE(red[i]);
  1621.                      }
  1622.                      break;
  1623.                   default:
  1624.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  1625.                }
  1626.         }
  1627.         break;
  1628.      case GL_BYTE:
  1629.             {
  1630.                GLbyte *dst = (GLbyte *) destination;
  1631.                switch (format) {
  1632.                   case GL_RED:
  1633.                      for (i=0;i<n;i++)
  1634.                         dst[i] = FLOAT_TO_BYTE(red[i]);
  1635.                      break;
  1636.                   case GL_GREEN:
  1637.                      for (i=0;i<n;i++)
  1638.                         dst[i] = FLOAT_TO_BYTE(green[i]);
  1639.                      break;
  1640.                   case GL_BLUE:
  1641.                      for (i=0;i<n;i++)
  1642.                         dst[i] = FLOAT_TO_BYTE(blue[i]);
  1643.                      break;
  1644.                   case GL_LUMINANCE:
  1645.                      for (i=0;i<n;i++)
  1646.                         dst[i] = FLOAT_TO_BYTE(luminance[i]);
  1647.                      break;
  1648.                   case GL_LUMINANCE_ALPHA:
  1649.                      for (i=0;i<n;i++) {
  1650.                         dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
  1651.                         dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]);
  1652.                      }
  1653.                      break;
  1654.                   case GL_RGB:
  1655.                      for (i=0;i<n;i++) {
  1656.                         dst[i*3+0] = FLOAT_TO_BYTE(red[i]);
  1657.                         dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
  1658.                         dst[i*3+2] = FLOAT_TO_BYTE(blue[i]);
  1659.                      }
  1660.                      break;
  1661.                   case GL_RGBA:
  1662.                      for (i=0;i<n;i++) {
  1663.                         dst[i*4+0] = FLOAT_TO_BYTE(red[i]);
  1664.                         dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
  1665.                         dst[i*4+2] = FLOAT_TO_BYTE(blue[i]);
  1666.                         dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
  1667.                      }
  1668.                      break;
  1669.                   case GL_BGR:
  1670.                      for (i=0;i<n;i++) {
  1671.                         dst[i*3+0] = FLOAT_TO_BYTE(blue[i]);
  1672.                         dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
  1673.                         dst[i*3+2] = FLOAT_TO_BYTE(red[i]);
  1674.                      }
  1675.                      break;
  1676.                   case GL_BGRA:
  1677.                      for (i=0;i<n;i++) {
  1678.                         dst[i*4+0] = FLOAT_TO_BYTE(blue[i]);
  1679.                         dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
  1680.                         dst[i*4+2] = FLOAT_TO_BYTE(red[i]);
  1681.                         dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
  1682.                      }
  1683.                   case GL_ABGR_EXT:
  1684.                      for (i=0;i<n;i++) {
  1685.                         dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]);
  1686.                         dst[i*4+1] = FLOAT_TO_BYTE(blue[i]);
  1687.                         dst[i*4+2] = FLOAT_TO_BYTE(green[i]);
  1688.                         dst[i*4+3] = FLOAT_TO_BYTE(red[i]);
  1689.                      }
  1690.                      break;
  1691.                   default:
  1692.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  1693.                }
  1694.             }
  1695.         break;
  1696.      case GL_UNSIGNED_SHORT:
  1697.             {
  1698.                GLushort *dst = (GLushort *) destination;
  1699.                switch (format) {
  1700.                   case GL_RED:
  1701.                      for (i=0;i<n;i++)
  1702.                         dst[i] = FLOAT_TO_USHORT(red[i]);
  1703.                      break;
  1704.                   case GL_GREEN:
  1705.                      for (i=0;i<n;i++)
  1706.                         dst[i] = FLOAT_TO_USHORT(green[i]);
  1707.                      break;
  1708.                   case GL_BLUE:
  1709.                      for (i=0;i<n;i++)
  1710.                         dst[i] = FLOAT_TO_USHORT(blue[i]);
  1711.                      break;
  1712.                   case GL_LUMINANCE:
  1713.                      for (i=0;i<n;i++)
  1714.                         dst[i] = FLOAT_TO_USHORT(luminance[i]);
  1715.                      break;
  1716.                   case GL_LUMINANCE_ALPHA:
  1717.                      for (i=0;i<n;i++) {
  1718.                         dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
  1719.                         dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]);
  1720.                      }
  1721.                      break;
  1722.                   case GL_RGB:
  1723.                      for (i=0;i<n;i++) {
  1724.                         dst[i*3+0] = FLOAT_TO_USHORT(red[i]);
  1725.                         dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
  1726.                         dst[i*3+2] = FLOAT_TO_USHORT(blue[i]);
  1727.                      }
  1728.                      break;
  1729.                   case GL_RGBA:
  1730.                      for (i=0;i<n;i++) {
  1731.                         dst[i*4+0] = FLOAT_TO_USHORT(red[i]);
  1732.                         dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
  1733.                         dst[i*4+2] = FLOAT_TO_USHORT(blue[i]);
  1734.                         dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
  1735.                      }
  1736.                      break;
  1737.                   case GL_BGR:
  1738.                      for (i=0;i<n;i++) {
  1739.                         dst[i*3+0] = FLOAT_TO_USHORT(blue[i]);
  1740.                         dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
  1741.                         dst[i*3+2] = FLOAT_TO_USHORT(red[i]);
  1742.                      }
  1743.                      break;
  1744.                   case GL_BGRA:
  1745.                      for (i=0;i<n;i++) {
  1746.                         dst[i*4+0] = FLOAT_TO_USHORT(blue[i]);
  1747.                         dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
  1748.                         dst[i*4+2] = FLOAT_TO_USHORT(red[i]);
  1749.                         dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
  1750.                      }
  1751.                      break;
  1752.                   case GL_ABGR_EXT:
  1753.                      for (i=0;i<n;i++) {
  1754.                         dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]);
  1755.                         dst[i*4+1] = FLOAT_TO_USHORT(blue[i]);
  1756.                         dst[i*4+2] = FLOAT_TO_USHORT(green[i]);
  1757.                         dst[i*4+3] = FLOAT_TO_USHORT(red[i]);
  1758.                      }
  1759.                      break;
  1760.                   default:
  1761.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  1762.                }
  1763.                if (ctx->Pack.SwapBytes) {
  1764.                   gl_swap2( (GLushort *) dst, n*n );
  1765.                }
  1766.             }
  1767.         break;
  1768.      case GL_SHORT:
  1769.             {
  1770.                GLshort *dst = (GLshort *) destination;
  1771.                switch (format) {
  1772.                   case GL_RED:
  1773.                      for (i=0;i<n;i++)
  1774.                         dst[i] = FLOAT_TO_SHORT(red[i]);
  1775.                      break;
  1776.                   case GL_GREEN:
  1777.                      for (i=0;i<n;i++)
  1778.                         dst[i] = FLOAT_TO_SHORT(green[i]);
  1779.                      break;
  1780.                   case GL_BLUE:
  1781.                      for (i=0;i<n;i++)
  1782.                         dst[i] = FLOAT_TO_SHORT(blue[i]);
  1783.                      break;
  1784.                   case GL_LUMINANCE:
  1785.                      for (i=0;i<n;i++)
  1786.                         dst[i] = FLOAT_TO_SHORT(luminance[i]);
  1787.                      break;
  1788.                   case GL_LUMINANCE_ALPHA:
  1789.                      for (i=0;i<n;i++) {
  1790.                         dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
  1791.                         dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]);
  1792.                      }
  1793.                      break;
  1794.                   case GL_RGB:
  1795.                      for (i=0;i<n;i++) {
  1796.                         dst[i*3+0] = FLOAT_TO_SHORT(red[i]);
  1797.                         dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
  1798.                         dst[i*3+2] = FLOAT_TO_SHORT(blue[i]);
  1799.                      }
  1800.                      break;
  1801.                   case GL_RGBA:
  1802.                      for (i=0;i<n;i++) {
  1803.                         dst[i*4+0] = FLOAT_TO_SHORT(red[i]);
  1804.                         dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
  1805.                         dst[i*4+2] = FLOAT_TO_SHORT(blue[i]);
  1806.                         dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
  1807.                      }
  1808.                      break;
  1809.                   case GL_BGR:
  1810.                      for (i=0;i<n;i++) {
  1811.                         dst[i*3+0] = FLOAT_TO_SHORT(blue[i]);
  1812.                         dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
  1813.                         dst[i*3+2] = FLOAT_TO_SHORT(red[i]);
  1814.                      }
  1815.                      break;
  1816.                   case GL_BGRA:
  1817.                      for (i=0;i<n;i++) {
  1818.                         dst[i*4+0] = FLOAT_TO_SHORT(blue[i]);
  1819.                         dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
  1820.                         dst[i*4+2] = FLOAT_TO_SHORT(red[i]);
  1821.                         dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
  1822.                      }
  1823.                   case GL_ABGR_EXT:
  1824.                      for (i=0;i<n;i++) {
  1825.                         dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]);
  1826.                         dst[i*4+1] = FLOAT_TO_SHORT(blue[i]);
  1827.                         dst[i*4+2] = FLOAT_TO_SHORT(green[i]);
  1828.                         dst[i*4+3] = FLOAT_TO_SHORT(red[i]);
  1829.                      }
  1830.                      break;
  1831.                   default:
  1832.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  1833.                }
  1834.                if (ctx->Pack.SwapBytes) {
  1835.                   gl_swap2( (GLushort *) dst, n*n );
  1836.                }
  1837.             }
  1838.         break;
  1839.      case GL_UNSIGNED_INT:
  1840.             {
  1841.                GLuint *dst = (GLuint *) destination;
  1842.                switch (format) {
  1843.                   case GL_RED:
  1844.                      for (i=0;i<n;i++)
  1845.                         dst[i] = FLOAT_TO_UINT(red[i]);
  1846.                      break;
  1847.                   case GL_GREEN:
  1848.                      for (i=0;i<n;i++)
  1849.                         dst[i] = FLOAT_TO_UINT(green[i]);
  1850.                      break;
  1851.                   case GL_BLUE:
  1852.                      for (i=0;i<n;i++)
  1853.                         dst[i] = FLOAT_TO_UINT(blue[i]);
  1854.                      break;
  1855.                   case GL_LUMINANCE:
  1856.                      for (i=0;i<n;i++)
  1857.                         dst[i] = FLOAT_TO_UINT(luminance[i]);
  1858.                      break;
  1859.                   case GL_LUMINANCE_ALPHA:
  1860.                      for (i=0;i<n;i++) {
  1861.                         dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
  1862.                         dst[i*2+1] = FLOAT_TO_UINT(alpha[i]);
  1863.                      }
  1864.                      break;
  1865.                   case GL_RGB:
  1866.                      for (i=0;i<n;i++) {
  1867.                         dst[i*3+0] = FLOAT_TO_UINT(red[i]);
  1868.                         dst[i*3+1] = FLOAT_TO_UINT(green[i]);
  1869.                         dst[i*3+2] = FLOAT_TO_UINT(blue[i]);
  1870.                      }
  1871.                      break;
  1872.                   case GL_RGBA:
  1873.                      for (i=0;i<n;i++) {
  1874.                         dst[i*4+0] = FLOAT_TO_UINT(red[i]);
  1875.                         dst[i*4+1] = FLOAT_TO_UINT(green[i]);
  1876.                         dst[i*4+2] = FLOAT_TO_UINT(blue[i]);
  1877.                         dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
  1878.                      }
  1879.                      break;
  1880.                   case GL_BGR:
  1881.                      for (i=0;i<n;i++) {
  1882.                         dst[i*3+0] = FLOAT_TO_UINT(blue[i]);
  1883.                         dst[i*3+1] = FLOAT_TO_UINT(green[i]);
  1884.                         dst[i*3+2] = FLOAT_TO_UINT(red[i]);
  1885.                      }
  1886.                      break;
  1887.                   case GL_BGRA:
  1888.                      for (i=0;i<n;i++) {
  1889.                         dst[i*4+0] = FLOAT_TO_UINT(blue[i]);
  1890.                         dst[i*4+1] = FLOAT_TO_UINT(green[i]);
  1891.                         dst[i*4+2] = FLOAT_TO_UINT(red[i]);
  1892.                         dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
  1893.                      }
  1894.                      break;
  1895.                   case GL_ABGR_EXT:
  1896.                      for (i=0;i<n;i++) {
  1897.                         dst[i*4+0] = FLOAT_TO_UINT(alpha[i]);
  1898.                         dst[i*4+1] = FLOAT_TO_UINT(blue[i]);
  1899.                         dst[i*4+2] = FLOAT_TO_UINT(green[i]);
  1900.                         dst[i*4+3] = FLOAT_TO_UINT(red[i]);
  1901.                      }
  1902.                      break;
  1903.                   default:
  1904.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  1905.                }
  1906.                if (ctx->Pack.SwapBytes) {
  1907.                   gl_swap4( (GLuint *) dst, n*n );
  1908.                }
  1909.             }
  1910.         break;
  1911.      case GL_INT:
  1912.         {
  1913.                GLint *dst = (GLint *) destination;
  1914.                switch (format) {
  1915.                   case GL_RED:
  1916.                      for (i=0;i<n;i++)
  1917.                         dst[i] = FLOAT_TO_INT(red[i]);
  1918.                      break;
  1919.                   case GL_GREEN:
  1920.                      for (i=0;i<n;i++)
  1921.                         dst[i] = FLOAT_TO_INT(green[i]);
  1922.                      break;
  1923.                   case GL_BLUE:
  1924.                      for (i=0;i<n;i++)
  1925.                         dst[i] = FLOAT_TO_INT(blue[i]);
  1926.                      break;
  1927.                   case GL_LUMINANCE:
  1928.                      for (i=0;i<n;i++)
  1929.                         dst[i] = FLOAT_TO_INT(luminance[i]);
  1930.                      break;
  1931.                   case GL_LUMINANCE_ALPHA:
  1932.                      for (i=0;i<n;i++) {
  1933.                         dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
  1934.                         dst[i*2+1] = FLOAT_TO_INT(alpha[i]);
  1935.                      }
  1936.                      break;
  1937.                   case GL_RGB:
  1938.                      for (i=0;i<n;i++) {
  1939.                         dst[i*3+0] = FLOAT_TO_INT(red[i]);
  1940.                         dst[i*3+1] = FLOAT_TO_INT(green[i]);
  1941.                         dst[i*3+2] = FLOAT_TO_INT(blue[i]);
  1942.                      }
  1943.                      break;
  1944.                   case GL_RGBA:
  1945.                      for (i=0;i<n;i++) {
  1946.                         dst[i*4+0] = FLOAT_TO_INT(red[i]);
  1947.                         dst[i*4+1] = FLOAT_TO_INT(green[i]);
  1948.                         dst[i*4+2] = FLOAT_TO_INT(blue[i]);
  1949.                         dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
  1950.                      }
  1951.                      break;
  1952.                   case GL_BGR:
  1953.                      for (i=0;i<n;i++) {
  1954.                         dst[i*3+0] = FLOAT_TO_INT(blue[i]);
  1955.                         dst[i*3+1] = FLOAT_TO_INT(green[i]);
  1956.                         dst[i*3+2] = FLOAT_TO_INT(red[i]);
  1957.                      }
  1958.                      break;
  1959.                   case GL_BGRA:
  1960.                      for (i=0;i<n;i++) {
  1961.                         dst[i*4+0] = FLOAT_TO_INT(blue[i]);
  1962.                         dst[i*4+1] = FLOAT_TO_INT(green[i]);
  1963.                         dst[i*4+2] = FLOAT_TO_INT(red[i]);
  1964.                         dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
  1965.                      }
  1966.                      break;
  1967.                   case GL_ABGR_EXT:
  1968.                      for (i=0;i<n;i++) {
  1969.                         dst[i*4+0] = FLOAT_TO_INT(alpha[i]);
  1970.                         dst[i*4+1] = FLOAT_TO_INT(blue[i]);
  1971.                         dst[i*4+2] = FLOAT_TO_INT(green[i]);
  1972.                         dst[i*4+3] = FLOAT_TO_INT(red[i]);
  1973.                      }
  1974.                      break;
  1975.                   default:
  1976.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  1977.                }
  1978.            if (ctx->Pack.SwapBytes) {
  1979.           gl_swap4( (GLuint *) dst, n*n );
  1980.            }
  1981.         }
  1982.         break;
  1983.      case GL_FLOAT:
  1984.         {
  1985.                GLfloat *dst = (GLfloat *) destination;
  1986.                switch (format) {
  1987.                   case GL_RED:
  1988.                      for (i=0;i<n;i++)
  1989.                         dst[i] = red[i];
  1990.                      break;
  1991.                   case GL_GREEN:
  1992.                      for (i=0;i<n;i++)
  1993.                         dst[i] = green[i];
  1994.                      break;
  1995.                   case GL_BLUE:
  1996.                      for (i=0;i<n;i++)
  1997.                         dst[i] = blue[i];
  1998.                      break;
  1999.                   case GL_LUMINANCE:
  2000.                      for (i=0;i<n;i++)
  2001.                         dst[i] = luminance[i];
  2002.                      break;
  2003.                   case GL_LUMINANCE_ALPHA:
  2004.                      for (i=0;i<n;i++) {
  2005.                         dst[i*2+0] = luminance[i];
  2006.                         dst[i*2+1] = alpha[i];
  2007.                      }
  2008.                      break;
  2009.                   case GL_RGB:
  2010.                      for (i=0;i<n;i++) {
  2011.                         dst[i*3+0] = red[i];
  2012.                         dst[i*3+1] = green[i];
  2013.                         dst[i*3+2] = blue[i];
  2014.                      }
  2015.                      break;
  2016.                   case GL_RGBA:
  2017.                      for (i=0;i<n;i++) {
  2018.                         dst[i*4+0] = red[i];
  2019.                         dst[i*4+1] = green[i];
  2020.                         dst[i*4+2] = blue[i];
  2021.                         dst[i*4+3] = alpha[i];
  2022.                      }
  2023.                      break;
  2024.                   case GL_BGR:
  2025.                      for (i=0;i<n;i++) {
  2026.                         dst[i*3+0] = blue[i];
  2027.                         dst[i*3+1] = green[i];
  2028.                         dst[i*3+2] = red[i];
  2029.                      }
  2030.                      break;
  2031.                   case GL_BGRA:
  2032.                      for (i=0;i<n;i++) {
  2033.                         dst[i*4+0] = blue[i];
  2034.                         dst[i*4+1] = green[i];
  2035.                         dst[i*4+2] = red[i];
  2036.                         dst[i*4+3] = alpha[i];
  2037.                      }
  2038.                      break;
  2039.                   case GL_ABGR_EXT:
  2040.                      for (i=0;i<n;i++) {
  2041.                         dst[i*4+0] = alpha[i];
  2042.                         dst[i*4+1] = blue[i];
  2043.                         dst[i*4+2] = green[i];
  2044.                         dst[i*4+3] = red[i];
  2045.                      }
  2046.                      break;
  2047.                   default:
  2048.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  2049.                }
  2050.            if (ctx->Pack.SwapBytes) {
  2051.           gl_swap4( (GLuint *) dst, n*n );
  2052.            }
  2053.         }
  2054.         break;
  2055.          case GL_UNSIGNED_BYTE_3_3_2:
  2056.             if (format == GL_RGB) {
  2057.                GLubyte *dst = (GLubyte *) destination;
  2058.                for (i=0;i<n;i++) {
  2059.                   dst[i] = (((GLint) (red[i]   * 7.0F)) << 5)
  2060.                          | (((GLint) (green[i] * 7.0F)) << 2)
  2061.                          | (((GLint) (blue[i]  * 3.0F))     );
  2062.                }
  2063.             }
  2064.             break;
  2065.          case GL_UNSIGNED_BYTE_2_3_3_REV:
  2066.             if (format == GL_RGB) {
  2067.                GLubyte *dst = (GLubyte *) destination;
  2068.                for (i=0;i<n;i++) {
  2069.                   dst[i] = (((GLint) (red[i]   * 7.0F))     )
  2070.                          | (((GLint) (green[i] * 7.0F)) << 3)
  2071.                          | (((GLint) (blue[i]  * 3.0F)) << 5);
  2072.                }
  2073.             }
  2074.             break;
  2075.          case GL_UNSIGNED_SHORT_5_6_5:
  2076.             if (format == GL_RGB) {
  2077.                GLushort *dst = (GLushort *) destination;
  2078.                for (i=0;i<n;i++) {
  2079.                   dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
  2080.                          | (((GLint) (green[i] * 63.0F)) <<  5)
  2081.                          | (((GLint) (blue[i]  * 31.0F))      );
  2082.                }
  2083.             }
  2084.             break;
  2085.          case GL_UNSIGNED_SHORT_5_6_5_REV:
  2086.             if (format == GL_RGB) {
  2087.                GLushort *dst = (GLushort *) destination;
  2088.                for (i=0;i<n;i++) {
  2089.                   dst[i] = (((GLint) (red[i]   * 31.0F))      )
  2090.                          | (((GLint) (green[i] * 63.0F)) <<  5)
  2091.                          | (((GLint) (blue[i]  * 31.0F)) << 11);
  2092.                }
  2093.             }
  2094.             break;
  2095.          case GL_UNSIGNED_SHORT_4_4_4_4:
  2096.             if (format == GL_RGB) {
  2097.                GLushort *dst = (GLushort *) destination;
  2098.                for (i=0;i<n;i++) {
  2099.                   dst[i] = (((GLint) (red[i]   * 15.0F)) << 12)
  2100.                          | (((GLint) (green[i] * 15.0F)) <<  8)
  2101.                          | (((GLint) (blue[i]  * 15.0F)) <<  4)
  2102.                          | (((GLint) (alpha[i] * 15.0F))      );
  2103.                }
  2104.             }
  2105.             break;
  2106.          case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  2107.             if (format == GL_RGB) {
  2108.                GLushort *dst = (GLushort *) destination;
  2109.                for (i=0;i<n;i++) {
  2110.                   dst[i] = (((GLint) (red[i]   * 15.0F))      )
  2111.                          | (((GLint) (green[i] * 15.0F)) <<  4)
  2112.                          | (((GLint) (blue[i]  * 15.0F)) <<  8)
  2113.                          | (((GLint) (alpha[i] * 15.0F)) << 12);
  2114.                }
  2115.             }
  2116.             break;
  2117.          case GL_UNSIGNED_SHORT_5_5_5_1:
  2118.             if (format == GL_RGB) {
  2119.                GLushort *dst = (GLushort *) destination;
  2120.                for (i=0;i<n;i++) {
  2121.                   dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
  2122.                          | (((GLint) (green[i] * 31.0F)) <<  6)
  2123.                          | (((GLint) (blue[i]  * 31.0F)) <<  1)
  2124.                          | (((GLint) (alpha[i] *  1.0F))      );
  2125.                }
  2126.             }
  2127.             break;
  2128.          case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  2129.             if (format == GL_RGB) {
  2130.                GLushort *dst = (GLushort *) destination;
  2131.                for (i=0;i<n;i++) {
  2132.                   dst[i] = (((GLint) (red[i]   * 31.0F))      )
  2133.                          | (((GLint) (green[i] * 31.0F)) <<  5)
  2134.                          | (((GLint) (blue[i]  * 31.0F)) << 10)
  2135.                          | (((GLint) (alpha[i] *  1.0F)) << 15);
  2136.                }
  2137.             }
  2138.             break;
  2139.          case GL_UNSIGNED_INT_8_8_8_8:
  2140.             if (format == GL_RGBA) {
  2141.                GLuint *dst = (GLuint *) destination;
  2142.                for (i=0;i<n;i++) {
  2143.                   dst[i] = (((GLint) (red[i]   * 255.0F)) << 24)
  2144.                          | (((GLint) (green[i] * 255.0F)) << 16)
  2145.                          | (((GLint) (blue[i]  * 255.0F)) <<  8)
  2146.                          | (((GLint) (alpha[i] * 255.0F))      );
  2147.                }
  2148.             }
  2149.             else if (format == GL_BGRA) {
  2150.                GLushort *dst = (GLushort *) destination;
  2151.                for (i=0;i<n;i++) {
  2152.                   dst[i] = (((GLint) (blue[i]  * 255.0F)) << 24)
  2153.                          | (((GLint) (green[i] * 255.0F)) << 16)
  2154.                          | (((GLint) (red[i]   * 255.0F)) <<  8)
  2155.                          | (((GLint) (alpha[i] * 255.0F))      );
  2156.                }
  2157.             }
  2158.             else if (format == GL_ABGR_EXT) {
  2159.                GLushort *dst = (GLushort *) destination;
  2160.                for (i=0;i<n;i++) {
  2161.                   dst[i] = (((GLint) (alpha[i] * 255.0F)) << 24)
  2162.                          | (((GLint) (blue[i]  * 255.0F)) << 16)
  2163.                          | (((GLint) (green[i] * 255.0F)) <<  8)
  2164.                          | (((GLint) (red[i]   * 255.0F))      );
  2165.                }
  2166.             }
  2167.             break;
  2168.          case GL_UNSIGNED_INT_8_8_8_8_REV:
  2169.             if (format == GL_RGBA) {
  2170.                GLuint *dst = (GLuint *) destination;
  2171.                for (i=0;i<n;i++) {
  2172.                   dst[i] = (((GLint) (red[i]   * 255.0F))      )
  2173.                          | (((GLint) (green[i] * 255.0F)) <<  8)
  2174.                          | (((GLint) (blue[i]  * 255.0F)) << 16)
  2175.                          | (((GLint) (alpha[i] * 255.0F)) << 24);
  2176.                }
  2177.             }
  2178.             else if (format == GL_BGRA) {
  2179.                GLushort *dst = (GLushort *) destination;
  2180.                for (i=0;i<n;i++) {
  2181.                   dst[i] = (((GLint) (blue[i]  * 255.0F))      )
  2182.                          | (((GLint) (green[i] * 255.0F)) <<  8)
  2183.                          | (((GLint) (red[i]   * 255.0F)) << 16)
  2184.                          | (((GLint) (alpha[i] * 255.0F)) << 24);
  2185.                }
  2186.             }
  2187.             else if (format == GL_ABGR_EXT) {
  2188.                GLushort *dst = (GLushort *) destination;
  2189.                for (i=0;i<n;i++) {
  2190.                   dst[i] = (((GLint) (alpha[i] * 255.0F))      )
  2191.                          | (((GLint) (blue[i]  * 255.0F)) <<  8)
  2192.                          | (((GLint) (green[i] * 255.0F)) << 16)
  2193.                          | (((GLint) (red[i]   * 255.0F)) << 24);
  2194.                }
  2195.             }
  2196.             break;
  2197.          case GL_UNSIGNED_INT_10_10_10_2:
  2198.             if (format == GL_RGBA) {
  2199.                GLuint *dst = (GLuint *) destination;
  2200.                for (i=0;i<n;i++) {
  2201.                   dst[i] = (((GLint) (red[i]   * 1023.0F)) << 22)
  2202.                          | (((GLint) (green[i] * 1023.0F)) << 12)
  2203.                          | (((GLint) (blue[i]  * 1023.0F)) <<  2)
  2204.                          | (((GLint) (alpha[i] *    3.0F))      );
  2205.                }
  2206.             }
  2207.             else if (format == GL_BGRA) {
  2208.                GLushort *dst = (GLushort *) destination;
  2209.                for (i=0;i<n;i++) {
  2210.                   dst[i] = (((GLint) (blue[i]  * 1023.0F)) << 22)
  2211.                          | (((GLint) (green[i] * 1023.0F)) << 12)
  2212.                          | (((GLint) (red[i]   * 1023.0F)) <<  2)
  2213.                          | (((GLint) (alpha[i] *    3.0F))      );
  2214.                }
  2215.             }
  2216.             else if (format == GL_ABGR_EXT) {
  2217.                GLushort *dst = (GLushort *) destination;
  2218.                for (i=0;i<n;i++) {
  2219.                   dst[i] = (((GLint) (alpha[i] * 1023.0F)) << 22)
  2220.                          | (((GLint) (blue[i]  * 1023.0F)) << 12)
  2221.                          | (((GLint) (green[i] * 1023.0F)) <<  2)
  2222.                          | (((GLint) (red[i]   *    3.0F))      );
  2223.                }
  2224.             }
  2225.             break;
  2226.          case GL_UNSIGNED_INT_2_10_10_10_REV:
  2227.             if (format == GL_RGBA) {
  2228.                GLuint *dst = (GLuint *) destination;
  2229.                for (i=0;i<n;i++) {
  2230.                   dst[i] = (((GLint) (red[i]   * 1023.0F))      )
  2231.                          | (((GLint) (green[i] * 1023.0F)) << 10)
  2232.                          | (((GLint) (blue[i]  * 1023.0F)) << 20)
  2233.                          | (((GLint) (alpha[i] *    3.0F)) << 30);
  2234.                }
  2235.             }
  2236.             else if (format == GL_BGRA) {
  2237.                GLushort *dst = (GLushort *) destination;
  2238.                for (i=0;i<n;i++) {
  2239.                   dst[i] = (((GLint) (blue[i]  * 1023.0F))      )
  2240.                          | (((GLint) (green[i] * 1023.0F)) << 10)
  2241.                          | (((GLint) (red[i]   * 1023.0F)) << 20)
  2242.                          | (((GLint) (alpha[i] *    3.0F)) << 30);
  2243.                }
  2244.             }
  2245.             else if (format == GL_ABGR_EXT) {
  2246.                GLushort *dst = (GLushort *) destination;
  2247.                for (i=0;i<n;i++) {
  2248.                   dst[i] = (((GLint) (alpha[i] * 1023.0F))      )
  2249.                          | (((GLint) (blue[i]  * 1023.0F)) << 10)
  2250.                          | (((GLint) (green[i] * 1023.0F)) << 20)
  2251.                          | (((GLint) (red[i]   *    3.0F)) << 30);
  2252.                }
  2253.             }
  2254.             break;
  2255.          default:
  2256.             gl_problem( ctx, "bad type in gl_pack_rgba_span" );
  2257.       }
  2258.    }
  2259. }
  2260.